package com.anolesoft.yr.mbus.code.service.impl;

import com.anolesoft.yr.exception.ServiceException;
import com.anolesoft.yr.mbus.base.dto.BaseCompanyDTO;
import com.anolesoft.yr.mbus.code.dto.CodeItemDTO;
import com.anolesoft.yr.mbus.code.entity.CodeItemDO;
import com.anolesoft.yr.mbus.code.mapper.CodeItemMapper;
import com.anolesoft.yr.mbus.code.service.CodeItemService;
import com.anolesoft.yr.util.CommonUtils;
import com.anolesoft.yr.util.CustomBeanUtils;
import com.anolesoft.yr.util.PageData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author zhj
 * @date 2020/9/21 11:33
 */
@Service
public class CodeItemServiceImpl implements CodeItemService {
    @Autowired
    private CodeItemMapper codeItemMapper;
    private void checkUnique(CodeItemDTO codeItemDTO, boolean isModify) {
        String objuid = codeItemDTO.getObjuid();
        String itemCodeName = codeItemDTO.getItemCodeName();
        if (isModify) {
            if (codeItemMapper.checkUniqueForItemCodeNameUpdate(itemCodeName, objuid) > 0) {
                throw new ServiceException("编码项名称已存在");
            }
        } else {
            if (codeItemMapper.checkUniqueForItemCodeName(itemCodeName) > 0) {
                throw new ServiceException("编码项名称已存在");
            }
        }
    }
    @Transactional
    @Override
    public int create(CodeItemDTO codeItemDTO) {
        checkUnique(codeItemDTO, false);
        return codeItemMapper.insert(CodeItemDO.builder()
                .itemCodeName(codeItemDTO.getItemCodeName())
                .objuid(codeItemDTO.getObjuid())
                .factoryCode(codeItemDTO.getFactoryCode())
                .trimAspect(codeItemDTO.getTrimAspect())
                .remark(codeItemDTO.getRemark())
                .modifyUserUid(codeItemDTO.getModifyUserUid())
                .modifyTime(codeItemDTO.getModifyTime())
                .itemType(codeItemDTO.getItemType())
                .itemPlaceholder(codeItemDTO.getItemPlaceholder())
                .itemLength(codeItemDTO.getItemLength())
                .itemGeneClass(codeItemDTO.getItemGeneClass())
                .itemFormat(codeItemDTO.getItemFormat())
                .itemDesc(codeItemDTO.getItemDesc())
                .deleteFlag(codeItemDTO.getDeleteFlag())
                .createUserUid(codeItemDTO.getCreateUserUid())
                .createTime(codeItemDTO.getCreateTime())
                .build());
    }

    @Transactional
    @Override
    public int modify(CodeItemDTO codeItemDTO) {
        checkUnique(codeItemDTO, true);
        CodeItemDO codeItemDO = codeItemMapper.selectOneById(codeItemDTO.getObjuid());
        if (codeItemDO == null) {
            codeItemDO = CodeItemDO.builder().build();
        }
        codeItemDO.setItemCodeName(codeItemDTO.getItemCodeName());
        codeItemDO.setObjuid(codeItemDTO.getObjuid());
        codeItemDO.setFactoryCode(codeItemDTO.getFactoryCode());
        codeItemDO.setTrimAspect(codeItemDTO.getTrimAspect());
        codeItemDO.setRemark(codeItemDTO.getRemark());
        codeItemDO.setModifyUserUid(codeItemDTO.getModifyUserUid());
        codeItemDO.setModifyTime(codeItemDTO.getModifyTime());
        codeItemDO.setItemType(codeItemDTO.getItemType());
        codeItemDO.setItemPlaceholder(codeItemDTO.getItemPlaceholder());
        codeItemDO.setItemLength(codeItemDTO.getItemLength());
        codeItemDO.setItemGeneClass(codeItemDTO.getItemGeneClass());
        codeItemDO.setItemFormat(codeItemDTO.getItemFormat());
        codeItemDO.setItemDesc(codeItemDTO.getItemDesc());
        codeItemDO.setDeleteFlag(codeItemDTO.getDeleteFlag());
        codeItemDO.setCreateUserUid(codeItemDTO.getCreateUserUid());
        codeItemDO.setCreateTime(codeItemDTO.getCreateTime());

        return codeItemMapper.update(codeItemDO);
    }

    @Transactional
    @Override
    public int remove(String id) {
        return codeItemMapper.delete(id);
    }

    @Transactional
    @Override
    public int removeBatch(List<String> ids) {
        int res = 0;
        for (String id : ids) {
            res += codeItemMapper.delete(id);
        }
        return res;
    }

    @Override
    public List<CodeItemDTO> selectAll() {
        return codeItemMapper.selectAll().stream().map(CustomBeanUtils::codeItemDO2DTO).collect(Collectors.toList());
    }

    @Override
    public Optional<CodeItemDTO> selectOneById(String id) {
        CodeItemDO codeItemDO = codeItemMapper.selectOneById(id);
        if (codeItemDO != null) {
            return Optional.of(CustomBeanUtils.codeItemDO2DTO(codeItemDO));
        }
        return Optional.empty();
    }

    @Override
    public PageData<List<CodeItemDTO>> selectList(CodeItemDTO codeItemDTO, Integer limit, Integer curr) {
        CodeItemDO codeItemDO = buildCondition(codeItemDTO);
        Page<CodeItemDO> page = new Page<>(curr, limit);
        List<CodeItemDTO> codeItemDTOS = codeItemMapper.selectPageList(codeItemDO, page).stream().map(CustomBeanUtils::codeItemDO2DTO).collect(Collectors.toList());
        return new PageData<>((int) page.getTotal(), codeItemDTOS);
    }

    private CodeItemDO buildCondition(CodeItemDTO codeItemDTO) {
        return CodeItemDO.builder()
                .factoryCode(CommonUtils.stringNull(codeItemDTO.getFactoryCode()))
                .itemCodeName(CommonUtils.warpStringNull(codeItemDTO.getItemCodeName()))
                .itemType(codeItemDTO.getItemType())
                .objuid(CommonUtils.stringNull(codeItemDTO.getObjuid()))
                .build();
    }
}
